package com.coder.bike.ai;

import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;

@Configuration
public class SeparateChatAssistantConfig {
    @Autowired
    private MongoChatMemoryStore mongoChatMemoryStore;

    @Bean
    ContentRetriever contentRetrieverXiangXiang() {
        Document document1 = FileSystemDocumentLoader.loadDocument("E:/knowledge/sharing/项目实现.md");
        Document document2 = FileSystemDocumentLoader.loadDocument("E:/knowledge/sharing/单车品牌介绍.md");
        Document document3 = FileSystemDocumentLoader.loadDocument("E:/knowledge/sharing/单车知识补充.md");
        Document document4 = FileSystemDocumentLoader.loadDocument("E:/knowledge/sharing/城市用户习惯.md");
        Document document5 = FileSystemDocumentLoader.loadDocument("E:/knowledge/sharing/信越信息科技有限公司.md");
        List<Document> documents = Arrays.asList(document1, document2, document3, document4, document5);
        InMemoryEmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();
        EmbeddingStoreIngestor.ingest(documents, embeddingStore);
        return EmbeddingStoreContentRetriever.from(embeddingStore);
    }

    @Bean
    public ChatMemoryProvider chatMemoryProvider() {
        return memoryId -> MessageWindowChatMemory.builder()
                .id(memoryId)
                .maxMessages(20)
                .chatMemoryStore(mongoChatMemoryStore)
                .build();
    }
}